package com.example.myservlet;

import com.example.anno.RequestMapping;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletRegistration;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@RequestMapping("/servlets")
public class ListServletsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ServletContext context = getServletContext();
        Map<String, ? extends ServletRegistration> registrations = context.getServletRegistrations();

        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();

        for (Map.Entry<String, ? extends ServletRegistration> entry : registrations.entrySet()) {
            out.println("Servlet Name: " + entry.getKey());
            out.println("Mappings: " + entry.getValue().getMappings());
            out.println("Class: " + entry.getValue().getClassName());
            out.println("------");
        }
    }
}